﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ext.Net;
using BO = Viewer.BusinessObject.BusinessEntities;
using Viewer.BusinessObject.BusinessEntities;
using System.Collections.Generic;
namespace Viewer.NhanSu
{

    public partial class NhanSu : System.Web.UI.Page
    {
        NSTrangThaiLamViec trangThai = new NSTrangThaiLamViec();
        NSQuaTrinhGianDoanBaoHiem baoHiem = new NSQuaTrinhGianDoanBaoHiem();
        NSNghiPhep nghiPhep = new NSNghiPhep();
        //NSCongViecChinh congViecChinh = new NSCongViecChinh();
        NSNgheNghiep ngheNghiep = new NSNgheNghiep();
        NSQuanHeGiaDinh quanHe = new NSQuanHeGiaDinh();
        NSDaoTao dt = new NSDaoTao();
        NSQuaTrinhCongTac qtct = new NSQuaTrinhCongTac();
        NSQuaTrinhCongTacDang quaTrinhCongTacDang = new NSQuaTrinhCongTacDang();
        NSNhanXet nhanXet = new NSNhanXet();
        NSKiLuat kiLuat = new NSKiLuat();
        NSNgoaiNgu ngoaiNgu = new NSNgoaiNgu();
        NSKhenThuong khenThuong = new NSKhenThuong();
        NSHopDongLaoDong hopdong = new NSHopDongLaoDong();
        NSPhuCapChucVu phuCapChucVu = new NSPhuCapChucVu();
        NSPhuCapKhac phucCapKhac = new NSPhuCapKhac();
        NSNgachBacLuong ngach = new NSNgachBacLuong();
        private void DeleteTableLienQuan(int nhanVienId)
        {
            trangThai.DeleletAllTrangThaiLamViecByNhanVien(nhanVienId);
            baoHiem.DeleletAllQTGDByNhanVien(nhanVienId);
            nghiPhep.DeleletAllNghiPhepByNhanVien(nhanVienId);
            //congViecChinh.DeleletAllCongViecChinhByNhanVien(nhanVienId);
            ngheNghiep.DeleletAllNgheNghiepByNhanVien(nhanVienId);
            quanHe.DeleletAllQHByNhanVien(nhanVienId);
            dt.DeleletAllDTByNhanVien(nhanVienId);
            qtct.DeleletAllQTCTByNhanVien(nhanVienId);
            nhanXet.DeleletAllNXByNhanVien(nhanVienId);
            kiLuat.DeleletAllKLByNhanVien(nhanVienId);
            ngoaiNgu.DeleletAllNgoaiNguByNhanVien(nhanVienId);
            khenThuong.DeleletAllKTByNhanVien(nhanVienId);
            hopdong.DeleletAllHDLD(nhanVienId);
            phuCapChucVu.DeleletAllPCCVByNhanVien(nhanVienId);
            phucCapKhac.DeleletAllPCKByNhanVien(nhanVienId);
            ngach.DeleletAllNBLByNhanVien(nhanVienId);
            quaTrinhCongTacDang.DeleletAllQTCTDangByNhanVien(nhanVienId);


        }

        BO.CommonCode common = new Viewer.BusinessObject.BusinessEntities.CommonCode();
        BO.NSToChuc query = new Viewer.BusinessObject.BusinessEntities.NSToChuc();
        BO.NSNhanVien nhanVien = new Viewer.BusinessObject.BusinessEntities.NSNhanVien();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadDataForTree();
                ResourceManager.GetInstance().RegisterIcon(Icon.Decline);
                btnAddNew.Disabled = true;
                LoadControl();
            }
        }
        protected void btnLookUpChoO_Click(object sender, DirectEventArgs e)
        {
            UCLookUpDiaChi.ShowDialogDiaChi();
        }
        protected void showPanel(object sender, DirectEventArgs e)
        {
            if (hdTimKiemNangCao.Value.Equals("0"))
            {
                CompositeField2.Hidden = true;
                hdTimKiemNangCao.Value = "1";
                CompositeField4.Hidden = true;
                //lnlTimKiemNangCao.Text = "Tìm kiếm nâng cao";
                lnlTimKiemNangCao.Icon = Icon.ZoomIn;
                PnSearch.Height = 45;
                Viewport1.DoLayout();
            }
            else
            {
                CompositeField2.Hidden = false;
                hdTimKiemNangCao.Value = "0";
                CompositeField4.Hidden = false;
                //lnlTimKiemNangCao.Text = "Tìm kiếm cơ bản";
                lnlTimKiemNangCao.Icon = Icon.ZoomOut;
                PnSearch.Height = 95;
                Viewport1.DoLayout();
            }
            X.Mask.Hide();
        }
        void LoadNgheNghiepTuyenDung()
        {
            string xmlFile = Server.MapPath("~/resources/DataCombobox_NgheNghiep.xml");
            DataSet ds = new DataSet();
            ds.ReadXml(xmlFile);

            //storeNgheNghiepTD.DataSource = ds.Tables["NgheNghiep"];
            //storeNgheNghiepTD.DataBind();

        }
        void LoadControl()
        {
            LoadNgheNghiepTuyenDung();
        }
        [DirectMethod]
        public void LoadGrid(string ID, string nodeName)
        {
            int id;
            try
            {
                id = int.Parse(ID);
            }
            catch (Exception)
            {

                id = 0;
            }
            hdNodeId.Text = id.ToString();
            hdNodeName.Text = nodeName;
            storeNhanVien.DataSource = nhanVien.LoadAllGridByToChucId(id);
            storeNhanVien.DataBind();
            X.Mask.Hide();

        }
        [DirectMethod]
        public void ClearData()
        {
            //cbbNgheNghiepDuocTD.SelectedItem.Value = string.Empty;
            cbbToanTuCongTac.SelectedItem.Value = string.Empty;
            cbbToanTuVaoDang.SelectedItem.Value = string.Empty;
            hdChoOThuongTruSearchId.Value = string.Empty;
            txtNam.Text = string.Empty;
            txtNamVaoDang.Text = string.Empty;
            txtHoTen.Text = string.Empty;
            txtSoHieuCBCC.Text = string.Empty;
            txtSoCMND.Text = string.Empty;
            ckbTrangThaiNghiHuu.Checked = false;
            X.Mask.Hide();
        }
        protected void btnSearch_Click(object sender, DirectEventArgs e)
        {
            int idNode = !string.IsNullOrEmpty(hdNodeId.Text) ? Convert.ToInt16(hdNodeId.Text) : 0;
            int namCongTac = !string.IsNullOrEmpty(txtNam.Text) ? Convert.ToInt16(txtNam.Text) : 0;
            string toanTuCongTac = !string.IsNullOrEmpty(cbbToanTuCongTac.SelectedItem.Text) ? cbbToanTuCongTac.SelectedItem.Text : string.Empty;
            int namVaoDang = !string.IsNullOrEmpty(txtNam.Text) ? Convert.ToInt16(txtNam.Text) : 0;
            string toanTuVaoDang = !string.IsNullOrEmpty(cbbToanTuVaoDang.SelectedItem.Text) ? cbbToanTuVaoDang.SelectedItem.Text : string.Empty;
            int diaChi = !string.IsNullOrEmpty(hdChoOThuongTruSearchId.Value.ToString()) ? Convert.ToInt32(hdChoOThuongTruSearchId.Value) : 0;
            bool checkNghiHuu = ckbTrangThaiNghiHuu.Checked;
            storeNhanVien.DataSource = nhanVien.SearchByCondition(idNode, txtHoTen.Text,  0, 0, toanTuCongTac, namCongTac, toanTuVaoDang, namVaoDang,  diaChi, checkNghiHuu, txtSoHieuCBCC.Text, txtSoCMND.Text);
            storeNhanVien.DataBind();
            X.Mask.Hide();
        }
        [DirectMethod]
        public void LoadDataForTree()
        {
            //Load Root                     
            Ext.Net.TreeNode root = new Ext.Net.TreeNode("Tổ Chức Hành Chính");
            root.Expanded = true;
            root.CustomAttributes.Add(new ConfigItem("IsView", "0"));//disable

            TreePanel.Root.Add(root);
            List<NSToChuc> lstToChuc = query.GetAll();
            List<NSToChuc> lstToChucLv1 = lstToChuc.Where(c => c.ToChucLevel == 1).ToList();

            foreach (var oToChucLv1 in lstToChucLv1)
            {
                var nodeLv1 = CreateNode(oToChucLv1);
                root.Nodes.Add(nodeLv1);
                List<NSToChuc> lstToChucLv2 = lstToChuc.Where(c => c.ToChucIDCha == oToChucLv1.ToChucID).ToList();
                foreach (var oToChucLv2 in lstToChucLv2)
                {
                    var nodeLv2 = CreateNode(oToChucLv2);
                    nodeLv1.Nodes.Add(nodeLv2);
                    List<NSToChuc> lstToChucLv3 = lstToChuc.Where(c => c.ToChucIDCha == oToChucLv2.ToChucID).ToList();
                    foreach (var oToChucLv3 in lstToChucLv3)
                    {
                        var nodeLv3 = CreateNode(oToChucLv3);
                        nodeLv2.Nodes.Add(nodeLv3);
                    }
                }
            }
            if (X.IsAjaxRequest)
            {
                Ext.Net.X.AddScript("refreshTree({0},{1});", TreePanel.ClientID, TreePanel.Root.ToJson());
            }
        }

        private List<QUNhomQuyenByToChuc_ToChuc> _nhomquyen;
        public List<QUNhomQuyenByToChuc_ToChuc> NhomQuyenByToChuc_ToChuc
        {
            get
            {
                if (_nhomquyen == null)
                {
                    _nhomquyen = (List<QUNhomQuyenByToChuc_ToChuc>)Session["NhomQuyenByToChuc_ToChuc"];
                }
                return _nhomquyen;
            }
        }

        /// <summary>
        /// Create TreeNode
        /// </summary>
        /// <param name="oToChuc"></param>
        /// <returns></returns>
        private Ext.Net.TreeNode CreateNode(NSToChuc oToChuc)
        {
            //1 nguoi thuoc nhieu nhom quyen
            if (this.NhomQuyenByToChuc_ToChuc == null)
            {
                if (Context.User.Identity.Name.ToLower() != "admin")
                {
                    return CreateDisableNode(oToChuc.ToChucID.ToString(), oToChuc.TenToChucFull);
                }
            }
            var lstNhomQuyenByToChuc_ToChuc = (from m in this.NhomQuyenByToChuc_ToChuc
                                               where m.ToChucID == oToChuc.ToChucID
                                               select m).ToList();
            if (lstNhomQuyenByToChuc_ToChuc.Count == 0)
            {
                //khong co quyen doi voi to chuc nay
                if (Context.User.Identity.Name.ToLower() != "admin")
                {
                    return CreateDisableNode(oToChuc.ToChucID.ToString(), oToChuc.TenToChucFull);
                }
            }
            bool IsView = false;
            bool IsAdd = false;
            bool IsEdit = false;
            bool IsDelete = false;

            foreach (var item in lstNhomQuyenByToChuc_ToChuc)
            {
                IsView = IsView || item.IsView;
                IsAdd = IsAdd || item.IsAdd;
                IsEdit = IsEdit || item.IsEdit;
                IsDelete = IsDelete || item.IsDelete;

            }

            //Hard code for admin 
            if (Context.User.Identity.Name.ToLower() == "admin")
            {
                IsView = true;
                IsAdd = true;
                IsEdit = true;
                IsDelete = true;
            }

            /////////////////
            if (Context.User.Identity.Name.ToLower() != "admin")
            {
                if (!IsView)
                {
                    return CreateDisableNode(oToChuc.ToChucID.ToString(), oToChuc.TenToChucFull);
                }
            }

            Ext.Net.TreeNode node = new Ext.Net.TreeNode(oToChuc.ToChucID.ToString(), oToChuc.TenToChucFull, Icon.Group);
            node.CustomAttributes.Add(new ConfigItem("IsView", IsView ? "1" : "0"));
            node.CustomAttributes.Add(new ConfigItem("IsAdd", IsAdd ? "1" : "0"));
            node.CustomAttributes.Add(new ConfigItem("IsEdit", IsEdit ? "1" : "0"));
            node.CustomAttributes.Add(new ConfigItem("IsDelete", IsDelete ? "1" : "0"));

            node.Expanded = true;
            return node;
        }

        /// <summary>
        /// Create a disable treenode
        /// </summary>
        /// <param name="id"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        private Ext.Net.TreeNode CreateDisableNode(string id, string text)
        {
            Ext.Net.TreeNode node = new Ext.Net.TreeNode(id + "Decline", text, Icon.GroupKey);
            node.CustomAttributes.Add(new ConfigItem("IsView", "0"));
            node.Disabled = true;
            node.Cls = "Disable";
            node.Expanded = true;
            return node;
        }

        public void LoadDataAllForGrid()
        {
            storeNhanVien.DataSource = nhanVien.LoadAllGrid();
            storeNhanVien.DataBind();
        }
        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            UCNhanVien.ShowDialog("AddNew", 0, hdNodeId.Text, hdNodeName.Text);
            X.Mask.Hide();
        }
        [DirectMethod]
        public void ShowDialog(string type, int ID)
        {
            if (type.Equals("Delete"))
            {
                DeleteTableLienQuan(ID);
                BO.NSNhanVien query = new Viewer.BusinessObject.BusinessEntities.NSNhanVien();
                query.DeleteNhanVien(ID);
                Ext.Net.X.AddScript(string.Format("storeNhanVien_RemoveById({0});", ID));
            }
            else if (type.Equals("Change"))
            {
                UCThayDoiCoQuanSuDung.ShowDialogThayDoiCoQuan(ID.ToString(), hdNodeName.Text, hdNodeId.Text);
            }
            else
            {
                UCNhanVien.ShowDialog(type, ID, hdNodeId.Text, hdNodeName.Text);
            }
            X.Mask.Hide();
        }
        protected void btnLookUp_Click(object sender, DirectEventArgs e)
        {
            UcToChuc.ShowLookUp_ToChuc("", false, "");
        }
        //protected void SelectNgheNghiep(object sender, DirectEventArgs e)
        //{
        //    storeChuyenNganh.DataSource = common.LoadDataByCodeIDCha(Convert.ToInt32(cbbNgheNghiepDuocTD.SelectedItem.Value));
        //    storeChuyenNganh.DataBind();
        //}
    }
}
